//
//  NewFeatureViewController.swift
//  Weibo
//
//  Created by apple on 15/12/10.
//  Copyright © 2015年 idcast. All rights reserved.
//

import UIKit
import SnapKit
//可重用的cell
private let WBNewFeatureViewCellId = "WBNewFeatureViewCellId"
//新特性的数量
private let WBNweFeatureCount = 4
class NewFeatureViewController: UICollectionViewController {
    //懒加载的时候 用var 前面用private
//   private lazy var layout = UICollectionViewLayout()
    //MARK:构造方法  设置布局
    init() {
        //必须要用UICollectionViewFlowLayout 要不然不会显示的
       let layout = UICollectionViewFlowLayout()
        
         layout.itemSize  = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        //构造函数 完成之后内部属性  才会被创建
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()

        //注册可重用的cell
        self.collectionView!.registerClass(NetFeatureCell.self, forCellWithReuseIdentifier: WBNewFeatureViewCellId)
   
    }


    // MARK: UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return WBNweFeatureCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WBNewFeatureViewCellId, forIndexPath: indexPath) as! NetFeatureCell
    //显示内容
        cell.imageIndex = indexPath.item
    
        return cell
    }
   override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
            let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
            if page != WBNweFeatureCount - 1 {
        
                return
            }
    
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NetFeatureCell
    
         cell.showButtonAnim()
        
    }

}

//MARK 新特性cell
private class NetFeatureCell: UICollectionViewCell {
    //图像属性
    private var imageIndex: Int = 0 {
    
        didSet {
        
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            //隐藏按钮
            startButton.hidden = true

        }
    }
    ///点击开始体验按钮
    @objc private func clickStartButton() {
    
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
    }
    
    //显示按钮动画
    private func showButtonAnim() {
    
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1.6, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                self.startButton.userInteractionEnabled = true
        }
    }
   //因为是父类的方法 所以前面要加 override
   override init(frame: CGRect) {
        super.init(frame: frame)
    
        setupUI()
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }

    private func setupUI() {
    
        //1 添加控件
        addSubview(iconView)
        addSubview(startButton)
        //以藏按钮不能在这里   因为有cell 的重用
//        startButton.hidden = true
        //2 指定位置
        iconView.frame = bounds
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
        }
        //设置监听方法
        startButton.addTarget(self, action: "clickStartButton", forControlEvents: .TouchUpInside)
    }
    
    //MARK 懒加载控件  属性
    private lazy var iconView: UIImageView = UIImageView()
    //按钮

    private lazy var startButton:UIButton = UIButton(title: "开始体验", backimageName: "new_feature_finish_button", color: UIColor.whiteColor())
}















